const JwtStrategy = require('passport-jwt').Strategy,
  ExtractJwt = require('passport-jwt').ExtractJwt;

const keys = require("../libs/common");
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.MD5_SUFFIX;

const mysql = require('mysql');
var db = mysql.createPool({ host: 'localhost', user: 'root', password: '123456', database: 'hdadmin' });

module.exports = passport => {
  passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
    console.log(jwt_payload)
    db.query(`SELECT * FROM admin_table WHERE admin_name='${jwt_payload.name}' && admin_state='true'`, (err, data) => {
      if (err) {
        res.status(500).send('数据库错误').end();
      }
      else {
        if (data.length == 0) {
          return done(null, false);
        }
        else {
          return done(null, data);
        }
      }
    })
  }));
}